import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import { getToken } from '@/utils/auth'
Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import nProgress from 'nprogress'
const whiteList = ['/login'] // 白名单 无需跳转
/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  
  {
    path: '/',
    component: () => import('@/views/chartshow/index.vue'),
    hidden: true
  },

  {
    path: '/login',
    component: () => import('@/views/login/login.vue'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  // {
  //   path: '/',
  //   component: Layout,
  //   redirect: '/dashboard',
  //   children: [{
  //     path: 'dashboard',
  //     name: 'Dashboard',
  //     component: () => import('@/views/dashboard/index'),
  //     meta: { title: '首页', icon: 'dashboard', affix: true }
  //   }]
  // }

  {
    path: '',
    component: Layout,
    redirect: '/home',
    children: [{
      // path: 'home',
      path: '/home',
      name: 'home',
      component: () => import('@/views/home/index'),
      meta: { title: '首页', icon: 'dashboard', affix: true }
    }]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    name: 'Person',
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: '个人中心', icon: 'user', noCache: true }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  // { path: '*',name: '404', redirect: '/404', hidden: true }
]




/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [

  {
    path: '/resourceview',
    component: Layout,
    redirect: '/resourceview/zyview',
    alwaysShow: true,
    name: 'Resourceview',
    meta: { title: '资源浏览', icon: 'clipboard' },
    children: [
      {
        path: 'zyview',
        name: 'zyview',
        component: () => import('@/views/resourceview/zyview'),
        meta: { title: '中药材资源浏览' }
      },
      {
        path: 'viewmessage',
        name: 'viewmessage',
        hidden: true,
        component: () => import('@/views/resourceview/viewmessage'),
        meta: { title: '药材详情' }
      },
      // {
      //   path: 'viewmessage/medicinalgone',
      //   name: 'medicinalgone',
      //   hidden: true,
      //   component: () => import('@/views/resourceview/viewmessage/medicinalgone'),
      //   meta: { title: '原料去向' }
      // }
    ]
  },
  {
    path: '/resourcemg',
    component: Layout,
    redirect: '/resourcemg/zymanage',
    alwaysShow: true,
    name: 'Resourcemg',
    meta: { title: '资源管理', icon: 'list', roles: ['admin', 'super_editor'] },
    children: [
      {
        path: 'zymanage',
        name: 'zymanage',
        component: () => import('@/views/resourcemg/zymanage'),
        meta: { title: '中药材资源管理', roles: ['admin', 'super_editor'] },
        children: [
          {
            path: 'productionarea',
            name: 'productionarea',
            hidden: true,// src\views\resourcemg\zymanage\productionArea\index.vue
            component: () => import('@/views/resourcemg/zymanage/productionArea/index.vue'),
            meta: { title: '生产基地', roles: ['admin', 'super_editor'] },
            children: [
              {
                path: 'medicinalgone',
                name: 'medicinalgone',
                hidden: true,
                component: () => import('@/views/resourcemg/zymanage/productionArea/medicinalgone'),
                meta: { title: '原料去向' }
              }
            ]
          }
        ]
      },
      {
        path: 'zyprice',
        name: 'zyprice',
        hidden: true,
        component: () => import('@/views/resourcemg/zymanage/showprice'),
        meta: { title: '药材价格', roles: ['admin', 'super_editor'] }
      },
      {
        path: 'areamanage',
        name: 'areamanage',
        component: () => import('@/views/resourcemg/areamanage'),
        meta: { title: '产地产区管理', roles: ['admin', 'super_editor'] }
      },
      {
        path: 'company',
        name: 'company',
        component: () => import('@/views/resourcemg/company'),
        meta: { title: '种植公司管理', roles: ['admin', 'super_editor'] }
      },
      {
        path: 'edition',
        name: 'edition',
        component: () => import('@/views/resourcemg/edition'),
        meta: { title: '归档信息管理', roles: ['admin', 'super_editor'] },
        children: [
          {
            path: 'editionContent',
            name: 'editionContent',
            hidden: true,
            component: () => import('@/views/resourcemg/edition/editionContent'),
            meta: { title: '归档详情' },
            children: [
              {
                path: 'medicinalEdition',
                name: 'medicinalEdition',
                hidden: true,
                component: () => import('@/views/resourcemg/edition/editionContent/medicinalEdition'),
                meta: { title: '中药材资源管理归档' },
                children: [
                  {
                    path: 'medicinalproductbaseEdition',
                    name: 'medicinalproductbaseEdition',
                    hidden: true,// src\views\resourcemg\zymanage\productionArea\index.vue
                    component: () => import('@/views/resourcemg/edition/editionContent/medicinalEdition/medicinalproductbaseEdition'),
                    meta: { title: '生产基地归档', roles: ['admin', 'super_editor'] },
                    children: [
                      {
                        path: 'medicinalgoneEdition',
                        name: 'medicinalgoneEdition',
                        hidden: true,
                        component: () => import('@/views/resourcemg/edition/editionContent/medicinalEdition/medicinalproductbaseEdition/medicinalgoneEdition'),
                        meta: { title: '原料去向归档' }
                      }
                    ]
                  }
                ]
              },
              {
                path: 'productbaseEdition',
                name: 'productbaseEdition',
                hidden: true,
                component: () => import('@/views/resourcemg/edition/editionContent/productbaseEdition'),
                meta: { title: '产地产区管理归档' }
              },
              {
                path: 'companyEdition',
                name: 'companyEdition',
                hidden: true,
                component: () => import('@/views/resourcemg/edition/editionContent/companyEdition'),
                meta: { title: '种植公司管理归档' }
              }
            ]
          }
        ]
      }
    ]
  },
  {
    path: '/basemanage',
    component: Layout,
    redirect: '/basemanage/user',
    alwaysShow: true,
    name: 'Basemanage',
    meta: { title: '基本信息管理', icon: 'peoples', roles: ['admin', 'editor'] },
    children: [
      {
        path: 'user',
        name: 'user',
        component: () => import('@/views/basemanage/user'),
        meta: { title: '用户管理', roles: ['admin', 'editor'] }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system/account',
    alwaysShow: true,
    name: 'System',
    meta: { title: '系统管理', icon: 'password', roles: ['admin'] },
    children: [
      {
        path: 'account',
        name: 'account',
        component: () => import('@/views/system/account'),
        meta: { title: '账号管理', roles: ['admin'] }
      },
      {
        path: 'role',
        name: 'role',
        component: () => import('@/views/system/role'),
        meta: { title: '角色管理', roles: ['admin'] }
      },
      {
        path: 'permission',
        name: 'permission',
        component: () => import('@/views/system/permission'),
        meta: { title: '权限管理', roles: ['admin'] }
      },
    ]
  },
  {
    path: '/terminal',
    component: Layout,
    redirect: '/terminal/devicemg',
    alwaysShow: true,
    name: 'Terminal',
    meta: { title: '终端管理', icon: 'international', roles: ['admin', 'editor'] },
    children: [
      {
        path: 'devicemg',
        name: 'devicemg',
        component: () => import('@/views/terminal/devicemg'),
        meta: { title: '设备管理', roles: ['admin', 'editor'] },
        children: [
          {
            path: 'devicedata',
            name: 'devicedata',
            hidden: true,
            component: () => import('@/views/terminal/devicemg/devicedata'),
            meta: { title: '设备数据', roles: ['admin', 'editor'] }
          }
        ]
      },
      // {
      //   path: 'environment',
      //   name: 'environment',
      //   component: () => import('@/views/terminal/environment/index.vue'),
      //   meta: { title: '总体环境监测', icon: 'dashboard', roles: ['admin', 'editor'] }
      // },
      {
        path: 'environment',
        name: 'environment',
        component: () => import('@/views/terminal/environment/echarts.vue'),
        meta: { title: '总体环境监测', roles: ['admin', 'editor'] }
      },
      {
        path: 'gateway',
        name: 'gateway',
        component: () => import('@/views/terminal/gateway'),
        meta: { title: '网关管理', roles: ['admin', 'super_editor'] }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*',name: '404', redirect: '/404', hidden: true }
]


//实例化vue的时候只挂载constantRouter
// export default new Router({
//   routes: constantRoutes
// });

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()


// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router